﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace CSharp.Common.Http
{
    public class HttpInfoHelper
    {
        private CookieCollection cookieInfo;
        private string encodeName;
        private Dictionary<string, string> postParameters;
        private string responseInfo;
        private string serverURL;
        private int timeout;

        public HttpInfoHelper()
        {
            this.serverURL = null;
            this.postParameters = null;
            this.cookieInfo = new CookieCollection();
            this.timeout = 0x7530;
            this.encodeName = "gb2312";
            this.responseInfo = null;
        }

        public HttpInfoHelper(string ServerURL)
        {
            this.serverURL = null;
            this.postParameters = null;
            this.cookieInfo = new CookieCollection();
            this.timeout = 0x7530;
            this.encodeName = "gb2312";
            this.responseInfo = null;
            this.serverURL = ServerURL;
        }

        public void Close()
        {
            this.postParameters.Clear();
        }

        
        private string GetHttpStreamInfo(Stream stream)
        {
            StringBuilder builder = new StringBuilder();
            Encoding encoding = Encoding.GetEncoding(this.encodeName);
            StreamReader reader = new StreamReader(stream, encoding);
            char[] buffer = new char[0x400];
            for (int i = reader.Read(buffer, 0, 0x400); i > 0; i = reader.Read(buffer, 0, 0x400))
            {
                builder.Append(buffer, 0, i);
            }
            reader.Close();
            return builder.ToString();
        }

        
        public string GetInfoFromProxyServer(string proxy, string targeturl)
        {
            WebProxy proxy2 = new WebProxy(proxy, true);
            WebRequest request = WebRequest.Create(targeturl);
            request.Proxy = proxy2;
            WebResponse response = request.GetResponse();
            return this.GetHttpStreamInfo(response.GetResponseStream());
        }

        
        public string GetInfoFromServer()
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string responseInfo;
            try
            {
                request = (HttpWebRequest) WebRequest.Create(this.serverURL);
                request.KeepAlive = true;
                request.Accept = "*/*";
                request.ContentType = "";
                request.Method = "GET";
                request.Timeout = this.timeout;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(this.cookieInfo);
                response = (HttpWebResponse) request.GetResponse();
                if (response.Cookies != null)
                {
                    this.cookieInfo.Add(response.Cookies);
                }
                this.responseInfo = this.GetHttpStreamInfo(response.GetResponseStream());
                responseInfo = this.responseInfo;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return responseInfo;
        }

        
        private string GetParametersString()
        {
            StringBuilder builder = new StringBuilder();
            foreach (KeyValuePair<string, string> pair in this.postParameters)
            {
                builder.Append(pair.Key + "=" + pair.Value.Replace(" ", "+") + "&");
            }
            return builder.ToString().Substring(0, builder.Length - 1);
        }

        
        public string PostAndGetInfoFromServer()
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string responseInfo;
            try
            {
                request = (HttpWebRequest) WebRequest.Create(this.serverURL);
                request.KeepAlive = true;
                request.Accept = "*/*";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                request.Timeout = this.timeout;
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(this.cookieInfo);
                byte[] bytes = Encoding.GetEncoding(this.encodeName).GetBytes(this.GetParametersString());
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                response = (HttpWebResponse) request.GetResponse();
                if (response.Cookies != null)
                {
                    this.cookieInfo.Add(response.Cookies);
                }
                this.responseInfo = this.GetHttpStreamInfo(response.GetResponseStream());
                responseInfo = this.responseInfo;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return responseInfo;
        }

        private bool RemoteCertificateValidationRoutine(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        public string EncodeName
        {
            get
            {
                return this.encodeName;
            }
            set
            {
                this.encodeName = value;
            }
        }

        public string LastServerResponseInfo
        {
            get
            {
                return this.responseInfo;
            }
        }

        public Dictionary<string, string> PostParameters
        {
            get
            {
                if (this.postParameters == null)
                {
                    this.postParameters = new Dictionary<string, string>();
                }
                return this.postParameters;
            }
        }

        public string ServerURL
        {
            get
            {
                return this.serverURL;
            }
            set
            {
                this.serverURL = value;
                if (this.postParameters != null)
                {
                    this.postParameters.Clear();
                }
            }
        }

        public int Timeout
        {
            get
            {
                return this.timeout;
            }
            set
            {
                this.timeout = value;
            }
        }
    }
}

